/*
 * Copyright (C) 2010 Grummfy
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package org.android.bbox.activities;

import org.android.bbox.Box;
import org.android.bbox.R;
import org.android.bbox.data.Profil;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Wifi extends Activity
{
	private TextView txt;
	private Profil profil;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.wifi_list);

		TextView title = (TextView) findViewById(R.id.myTitle);
		this.txt = (TextView) findViewById(R.id.myTxt);

		title.setText(R.string.wifi_title);
		this.txt.setText(R.string.wifi_empty);
	}

	public void onStart()
	{
		super.onStart();
		//TODO get access to the current data ....
		//this.profil = new Profil(1);

		this.updateWifiClient();
	}

	protected void updateWifiClient()
	{
		Box b = new Box(this.profil);
		String html = b.getHtmlString();

		try
		{
			int status_associated = html.indexOf("\"status\">", html.indexOf("Associated clients")) + 9;
			int number_client = Integer.decode(html.substring(status_associated, html.indexOf("&nbsp;", status_associated)));
			//System.out.println("nbr client : " + number_client);
			//String.format("%4d", i * j);

			if (number_client == 0)
			{
				this.txt.setText(R.string.wifi_empty);
				return;
			}

			int status_client = html.indexOf("\"status\">", html.indexOf("Clients MAC Address")) + 9;
			//String title = html.substring(status_client, html.indexOf("<", status_client));
			//title = title.replace("<br>", "\n");
			//System.out.println(title);

			int connected = html.indexOf("connected", status_client) + 9;
			String list_mac = html.substring(connected, html.indexOf("&nbsp;", connected));
			list_mac = list_mac.replace("<br>", "\n");
			//System.out.println(list_mac);

			this.txt.setText(String.format(getResources().getString(R.string.wifi_nbr_client), number_client) + "\n-----"+ list_mac);
		}
		catch (Exception e)
		{
			Log.e("BACKGROUND_PROC", e.getMessage());
			this.txt.setText(R.string.wifi_error_support);
		}
	}
}
